class Admin::AlbumController  < Base::AlbumBaseController
  #before_filter :login_required
  layout "admin"
  
  in_place_edit_for :photo,:title
  
  def index
    @albums=Album.paginate_all(params[:page])
  end
  
  #增加相册
  def create_album
    @album=Album.new(params[:album])
    if request.post? and @album.save
      flash.now[:notice]="相册:' #{@album.name}'创建成功!" 
      redirect_to :action=>:index
    else
      render :action=>'new_album'
    end
  end
  
  #查看相册
  def view_album
    @album=Album.find(params[:id])
    @photo=Photo.new
    @photo.album=@album
  end
  
  #编辑相册
  def edit_album
    @album=Album.find(params[:id])   
  end
  
  #update album
  def update_album
    @album=Album.find(params[:id])   
    if @album.update_attributes(params[:album])
      redirect_to :action=>:index
    else
      redirect_to :actoin=>:edit_album
    end
  end
  
  #delete an album
  def delete_album
    album=Album.find(params[:id])   
    album.destroy
    flash[:notice]="相册：#{album.name}被删除!"
    redirect_to :action=>:index
  end
  
  #show album on index page
  def show_on_index
    Album.update(params[:id],:show_on_index=>true)
    redirect_to :action=>:view_album,:id=>params[:id]
  end
  
  #list photos in an album
  def list_photos
    @album=Album.find(params[:id])
    @photos=@album.photos
  end
  
  #upload a photo
  def upload_photo
    @album=Album.find(params[:id])
    if @album.photos<<Photo.new(params[:photo])
      flash[:notice]="照片成功保存" 
    end
    redirect_to :action=>:view_album,:id=>@album
  end
  
  #删除照片
  def delete_photo
    photo=Photo.find(params[:id])
    album=photo.album
    photo.destroy
    album.photos(:refresh)
    flash[:notic]="相片:#{photo.title}被删除!"
    redirect_to(:action=>:view_album, :id=>album)  
  end
  
  #删除评论
  def delete_comment
    comment=Comment.find(params[:id])
    @photo=comment.resource
    comment.destroy
    @photo.comments(:refresh)
    comments_pagination(@photo.id)
  end
    
  def get_photo_title
    title=Photo.find(params[:id]).title
    render :text=>title
  end
end